package com.personal.dao.impl;

import java.io.InputStream;
import java.util.List;

import com.personal.dao.IUserDao;
import com.personal.dao.model.User;
import com.personal.persistence.io.Resources;
import com.personal.persistence.sqlSession.SqlSession;
import com.personal.persistence.sqlSession.SqlSessionFactory;
import com.personal.persistence.sqlSession.SqlSessionFactoryBuilder;

public class UserDaoImpl implements IUserDao {
    @Override
    public List<User> findAll() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession.findAll("user.findAll");
    }

    @Override
    public User findByCondition(User user) throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession.findByCondition("user.findByCondition", user);
    }
}
